<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/10/13
 * Time: 15:35
 */

namespace app\api\service;


use think\Cache;

class Huaweicloud
{
    const AK = 'IDF2O5XSKSVHF43ZVH6Q';
    const SK = 'VFA1mIXDrDEPidI6FMMKeIEGoujacprFb8gBqPqP';

    /**
     * 获取IAM用户Token
     * @return bool|mixed
     */
    public static function createToken()
    {
        $token = Cache::get('iam_token');
        if (!$token) {
            $url = 'https://iam.cn-east-2.myhuaweicloud.com/v3/auth/tokens';
            $params = json_encode([
                'auth' => [
                    'identity' => [
                        'methods' => ["password"],
                        'password' => [
                            'user' => [
                                'domain' => [
                                    'id' => '08a068e1f580f5970f5ec0038195e7c0'
                                ],
                                'name' => 'zycloudmarket',
                                'password' => 'zyw.2014'
                            ]
                        ]
                    ],
                    'scope' => [
                        'domain' => [
                            'id' => '08a068e1f580f5970f5ec0038195e7c0'
                        ]
                    ]
                ]
            ]);
            $result = https_request($url, $params);
            $data = json_decode($result, true);
            if (!isset($data['token'])) return false;
            $token = $data['token'];
            Cache::set('iam_token', $token, 86400);
        }
        return $token;
    }
}